import { getDictByKeys } from '@/api/dict'

const state = () => ({
  certType: [], // 证件类型
  carColor: [], // 车辆颜色
  openWay: [], // 开门方式
  openStatus: [], // 开门状态
  deviceStatus: [], // 设备状态
  nationList: [], // 民族
  countryList: [], // 国籍
  policyRole: [], // 政治面貌
  carBrand: [], // 车辆品牌
  carType: [], // 车辆类型
  parkType: [], // 停车类型
  sexType: [], // 性别
  cellCode: [], // 小区编码
  deviceType: [], // 设备类型
  roomType: [], // 房屋类型
  plateColor: [], // 车牌颜色
  auditStatus: [], // 审核状态
})
const matchJson = {
  CERT_TYPE: 'setCertType',
  CAR_BODY_COLOR: 'setCarColor',
  DOOR_OPEN_STATUS: 'setOpenStatus',
  DOOR_OPEN_STYLE: 'setOpenWay',
  DEVICE_STATUS: 'setDeviceStatus',
  NATION: 'setNationList',
  COUNTRY: 'setCountryList',
  POLITIC: 'setPolicyRole',
  CAR_BRAND: 'setCarBrand',
  SEX: 'setSexType',
  CELL_CODE: 'setCellCode',
  DEVICE_TYPE: 'setDeviceType',
  ROOM_NUM: 'setRoomType',
  CAR_PLATE_COLOR: 'setPlateColor',
  RESIDENT_STATUS: 'setAuditStatus',
  CAR_TYPE: 'setCarType',
}

function valueLabelJson(arr) {
  let v2k = {}
  arr.forEach((v) => {
    if (v) {
      v2k[v.value] = v.label
    }
  })
  return v2k
}

const getters = {
  certType: (state) => state.certType,
  certTypeJson: (state) => valueLabelJson(state.certType),
  carColor: (state) => state.carColor,
  carColorJson: (state) => valueLabelJson(state.carColor),
  openWay: (state) => state.openWay,
  openStatus: (state) => state.openStatus,
  deviceStatus: (state) => state.deviceStatus,
  nationList: (state) => state.nationList,
  nationListJson: (state) => valueLabelJson(state.nationList),
  countryList: (state) => state.countryList,
  countryListJson: (state) => valueLabelJson(state.countryList),
  policyRole: (state) => state.policyRole,
  policyRoleJson: (state) => valueLabelJson(state.policyRole),
  carBrand: (state) => state.carBrand,
  carBrandJson: (state) => valueLabelJson(state.carBrand),
  carType: (state) => state.carType,
  carTypeJson: (state) => valueLabelJson(state.carType),
  parkType: (state) => state.parkType,
  sexType: (state) => state.sexType,
  sexTypeJson: (state) => valueLabelJson(state.sexType),
  cellCode: (state) => state.cellCode,
  cellCodeJson: (state) => valueLabelJson(state.cellCode),
  deviceType: (state) => state.deviceType,
  roomType: (state) => state.roomType,
  plateColor: (state) => state.plateColor,
  auditStatus: (state) => state.auditStatus,
  auditStatusJson: (state) => valueLabelJson(state.auditStatus),
}
const mutations = {
  setCertType(state, x) {
    state.certType = x
  },
  setCarColor(state, x) {
    state.carColor = x
  },
  setOpenWay(state, x) {
    state.openWay = x
  },
  setOpenStatus(state, x) {
    state.openStatus = x
  },
  setDeviceStatus(state, x) {
    state.deviceStatus = x
  },
  setNationList(state, x) {
    state.nationList = x
  },
  setCountryList(state, x) {
    state.countryList = x
  },
  setPolicyRole(state, x) {
    state.policyRole = x
  },
  setCarBrand(state, x) {
    state.carBrand = x
  },
  setCarType(state, x) {
    state.carType = x
  },
  setParkType(state, x) {
    state.parkType = x
  },
  setSexType(state, x) {
    state.sexType = x
  },
  setCellCode(state, x) {
    state.cellCode = x
  },
  setDeviceType(state, x) {
    state.deviceType = x
  },
  setRoomType(state, x) {
    state.roomType = x
  },
  setPlateColor(state, x) {
    state.plateColor = x
  },
  setAuditStatus(state, x) {
    state.auditStatus = x
  },
}
const actions = {
  setCertType({ commit }, x) {
    commit('setCertType', x)
  },
  setCarColor({ commit }, x) {
    commit('setCarColor', x)
  },
  setOpenWay({ commit }, x) {
    commit('setOpenWay', x)
  },
  setOpenStatus({ commit }, x) {
    commit('setOpenStatus', x)
  },
  setDeviceStatus({ commit }, x) {
    commit('setDeviceStatus', x)
  },
  setNationList({ commit }, x) {
    commit('setNationList', x)
  },
  setCountryList({ commit }, x) {
    commit('setCountryList', x)
  },
  setPolicyRole({ commit }, x) {
    commit('setPolicyRole', x)
  },
  setCarBrand({ commit }, x) {
    commit('setCarBrand', x)
  },
  setCarType({ commit }, x) {
    commit('setCarType', x)
  },
  setParkType({ commit }, x) {
    commit('setParkType', x)
  },
  setSexType({ commit }, x) {
    commit('setSexType', x)
  },
  setCellCode({ commit }, x) {
    commit('setCellCode', x)
  },
  setDeviceType({ commit }, x) {
    commit('setDeviceType', x)
  },
  setRoomType({ commit }, x) {
    commit('setRoomType', x)
  },
  setPlateColor({ commit }, x) {
    commit('setPlateColor', x)
  },
  setAuditStatus({ commit }, x) {
    commit('setAuditStatus', x)
  },
  // 查询系统依赖的字典
  async fetchDict({ commit, state }, payload) {
    if (state.sexType.length > 0) {
      return
    }
    try {
      const res = await getDictByKeys(payload)
      if (res && res.data) {
        Object.keys(res.data).forEach((k) => {
          if (Array.isArray(res.data[k])) {
            commit(matchJson[k], res.data[k])
          }
        })
      }
    } catch (e) {
      console.log(e)
    }
  },
}
export default { state, getters, mutations, actions }
